package com.yichi.service;

import android.util.Log;

import com.yichi.device_bean.BatteryStore;
import com.yichi.device_bean.BmsParam;
import com.yichi.device_bean.BmsStatus;
import com.yichi.global.AppConfig;
import com.yichi.manager.HttpManager;
import com.yichi.manager.OnHttpResponseListener;
import com.yichi.util.JsonUtil;
import com.yichi.util.MapUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;

/**
 * 云平台服务
 */
public class PlatformService {

    private final static String TAG = "PlatformService";

    //测试链接
    public static void testConn(String baseUrl, final OnHttpResponseListener listener) throws Exception {
        String path = "/share/cabinet/getCabinetInfoByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", AppConfig.DEVICE_ID);
        HttpManager.getInstance().getAsync(baseUrl + path, paramMap, listener);
    }

    //获取设备信息
    public static void getCabinetInfoByNum(final OnHttpResponseListener listener) throws Exception {
        String path = "/share/cabinet/getCabinetInfoByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", AppConfig.DEVICE_ID);
        HttpManager.getInstance().getAsync(AppConfig.PLATFORM_BASEURL + path, paramMap, listener);
    }

    //获取token测试用
    public static void getToken(final OnHttpResponseListener listener) throws Exception {
        String path = "/share/miniProgram/getToken";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", 230);
        HttpManager.getInstance().getAsync(AppConfig.PLATFORM_BASEURL + path, paramMap, listener);
    }

    //电柜调用后台生成二维码
    public static void getCabinetQR(String deviceNo, final OnHttpResponseListener listener) {
        String path = "/share/miniProgram/getCabinetQR";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", deviceNo);
//        paramMap.put("page", "pages/index/index");
        paramMap.put("page", "pagesA/cabinet/detail");
        paramMap.put("envVersion", "trial");
        try {
            HttpManager.getInstance().getAsync(AppConfig.PLATFORM_BASEURL + path, paramMap, listener);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //获取换电柜信息
    public static void getDeviceInfo(String deviceNo) throws Exception {
        String path = "/share/cabinet/getCabinetInfoByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("no", deviceNo);
        String s = HttpManager.getInstance().get(AppConfig.PLATFORM_BASEURL + path, paramMap);
    }

    //获取广告信息
    public static void getAdInfo(String deviceNo) throws Exception {
        String path = "/share/banner/cabinetList";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("no", deviceNo);
        String s = HttpManager.getInstance().get(AppConfig.PLATFORM_BASEURL + path, paramMap);
    }

    /**
     * 获取BMS信息
     *
     * @param batteryNo
     */
    public static Map<String, Object> getBmsInfo(String batteryNo) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/battery/getByBatteryBmsNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("batteryBmsNum", batteryNo);
        try {
            String s = HttpManager.getInstance().get(path, paramMap);
            Map<String, Object> map = JsonUtil.parseMap(s);

            Integer code = MapUtil.getInteger(map, "code");
            String msg = MapUtil.getString(map, "msg");
            if (!code.equals(200)) {
                Log.e(TAG, "获取平台bms信息失败:" + msg);
                return null;
            }
            Map<String, Object> data = (Map<String, Object>) MapUtil.getMap(map, "data");

            Map<String, Object> batteryData = (Map<String, Object>) data.get("batteryData");
            BmsParam bmsParam = new BmsParam();
            bmsParam.setBatteryNo(batteryNo);
            //充放电电流，没用
            String current = MapUtil.getString(batteryData, "current");
            current = current.replace("A(charging)", "");
            Float floatCurrent = Float.parseFloat(current);
            bmsParam.setRechargeCurrent(floatCurrent);
            //soc
            String remainingCapacity = MapUtil.getString(batteryData, "remainingCapacity");
            remainingCapacity = remainingCapacity.replace("Ah", "");
            Double remainingCapacityDouble = Double.parseDouble(remainingCapacity);
            String totalCapacity = MapUtil.getString(batteryData, "totalCapacity");
            totalCapacity = totalCapacity.replace("Ah", "");
            Double totalCapacityFloat = Double.parseDouble(totalCapacity);
            Double socDouble = (remainingCapacityDouble / totalCapacityFloat) * 100;
            Integer soc = (int) Math.round(socDouble);
            bmsParam.setSoc(soc);
            //soh
            Integer loopTimes = MapUtil.getInteger(batteryData, "loopTimes");
            Integer soh = (int) Math.round((3000 - loopTimes) / 3000);
            soh = soh * 100;
            bmsParam.setSoh(soh);

            Map<String, Object> batteryStatus = (Map<String, Object>) data.get("batteryStatus");
            BmsStatus bmsStatus = new BmsStatus();
            bmsStatus.setCdzt(MapUtil.getInteger(batteryStatus, "isCharging"));

            Map<String, Object> result = new HashMap<>();
            result.put("bmsParam", bmsParam);
            result.put("bmsStatus", bmsStatus);
            return result;
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return null;
    }

    //获取维护工单
    public static void getOpsOrder(String code) throws Exception {
        String path = "/share/workOrderWarning/getListByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("currentCabinetNum", AppConfig.DEVICE_ID);
        paramMap.put("code", code);
        String s = HttpManager.getInstance().get(AppConfig.PLATFORM_BASEURL + path, paramMap);
    }

    //提交维护工单
    public static void submitOpsOrder() throws Exception {
        String path = "/share/workOrderWarning/tackleBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderNo", "");//工单编号
        String s = HttpManager.getInstance().postJson(AppConfig.PLATFORM_BASEURL + path, paramMap);
    }

    //取电：修改订单状态为：已取走电池
    public static void updateIsTakeBattery(String orderId) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/workOrderWarning/tackleBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderId", orderId);
        try {
            String s = HttpManager.getInstance().postJson(path, paramMap);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //取电：修改订单状态为：已关仓门
    public static void updateTakeCloseDoor(String orderId) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/tradeOrder/updateTakeCloseDoor";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderId", orderId);
        try {
            String s = HttpManager.getInstance().postJson(path, paramMap);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //还电：修改订单状态为：已放入电池
    public static void updateIsReturnBattery(String orderId) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/tradeOrder/updateIsReturnBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderId", orderId);
        try {
            String s = HttpManager.getInstance().postJson(path, paramMap);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //还电：修改订单状态为：已关仓门
    public static void updateReturnClose(String orderId) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/tradeOrder/updateReturnClose";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderId", orderId);
        try {
            String s = HttpManager.getInstance().postJson(path, paramMap);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //电柜调用后台生成二维码
    public static void getListByNumSync(String cabinetNum, Integer opsUserId, String passWord, final OnHttpResponseListener listener) {
        String path = AppConfig.PLATFORM_BASEURL + "/share/workOrderWarning/getListByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", cabinetNum);
        paramMap.put("opsUserId", opsUserId);
        paramMap.put("passWord", passWord);
        try {
            HttpManager.getInstance().getAsync(path, paramMap, listener);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    //获取维护订单
    public static Map<String, Object> getListByNum(String cabinetNum, Integer opsUserId, String passWord) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/workOrderWarning/getListByNum";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", cabinetNum);
        paramMap.put("opsUserId", opsUserId);
        paramMap.put("passWord", passWord);
        try {
            String s = HttpManager.getInstance().get(path, paramMap);
            Map<String, Object> map = JsonUtil.parseMap(s);
            Integer code = MapUtil.getInteger(map, "code");
            String msg = MapUtil.getString(map, "msg");
            if (code != 200) {
                throw new Exception(msg);
            }

            List<Map<String, Object>> rows = (List<Map<String, Object>>) map.get("rows");
            if (rows.isEmpty()) {
                throw new Exception("工单未找到");
            }
            Map<String, Object> firstRow = rows.get(0);
            return firstRow;
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
            throw e;
        }
    }

    //提交维护工单，异步
    public static void tackleBatteryAsync(Integer orderId, List<String> batteryNums, final OnHttpResponseListener listener) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/workOrderWarning/tackleBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", orderId);
        paramMap.put("batteryNums", batteryNums);
        HttpManager.getInstance().postJsonAsync(path, paramMap, listener);
    }

    //提交维护工单
    public static void tackleBattery(String orderId, List<String> batteryNums) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/workOrderWarning/tackleBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderId", orderId);
        paramMap.put("batteryNums", batteryNums);
        try {
            String s = HttpManager.getInstance().postJson(path, paramMap);
            Map<String, Object> map = JsonUtil.parseMap(s);
            Integer code = MapUtil.getInteger(map, "code");
            String msg = MapUtil.getString(map, "msg");
            if (code != 200) {
                throw new Exception(msg);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    //确认取电
    public static void addCabinetTakeBattery(BatteryStore batteryStore, final OnHttpResponseListener listener) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/tradeOrder/addCabinetTakeBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", AppConfig.DEVICE_ID);
        paramMap.put("takeBattery", batteryStore.getStoreNo());
        HttpManager.getInstance().postJsonAsync(path, paramMap, listener);
    }

    //确认存电
    public static void updateCabinetStillBattery(BatteryStore batteryStore, final OnHttpResponseListener listener) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/tradeOrder/updateCabinetStillBattery";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cabinetNum", AppConfig.DEVICE_ID);
        paramMap.put("stillBattery", batteryStore.getStoreNo());
        HttpManager.getInstance().postJsonAsync(path, paramMap, listener);
    }

    //人脸识别
    public static void searchUserFace(String base64Image, final OnHttpResponseListener listener) throws Exception {
        String path = AppConfig.PLATFORM_BASEURL + "/share/miniProgram/searchUserFace";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("image", base64Image);
        HttpManager.getInstance().postJsonAsync(path, paramMap, listener);
    }
}
